﻿Imports MeoTom.Core.BLL
Imports System.Text.RegularExpressions
Imports MeoTom.Core

Partial Public Class PageComment
    Inherits UI.Page
    Private ReadOnly _util As New Utilities.Common()
    Private ReadOnly _objComment As New Comments()


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        Dim previousPage As String
        If Request.QueryString.ToString = vbNullString Then
            previousPage = Server.UrlEncode(Request.ServerVariables("PATH_INFO"))
        Else
            previousPage = Server.UrlEncode(Request.ServerVariables("PATH_INFO") & "?" & Request.QueryString.ToString())
        End If
        _util.ValidateLogin(previousPage)

        BindGrid()
    End Sub

    Public Sub BindGrid()
        If Request.QueryString("action") = "Search" Then
            _objComment.KeyWord = _util.NoHTML(Request.QueryString("s"))
            grvComments.DataSource = _objComment.Find()
            grvComments.DataBind()
        ElseIf Request.QueryString("action") = "getByArticleID" Then
            _objComment.ArticleId = Integer.Parse(Request.QueryString("ArticleID"))
            grvComments.DataSource = _objComment.GetByArticle()
            grvComments.DataBind()
        ElseIf Request.QueryString("action") = "Delete" Then
            _objComment.CommentsID = Integer.Parse(Request.QueryString("CommentsID"))
            _objComment.Delete()
            'objComment.ArticleId = Integer.Parse(Request.QueryString("ArticlesId"))
            Response.Redirect("/mt-inside/PageComment.aspx", False)
        ElseIf Request.QueryString("action") = "UpdateStatus" Then
            _objComment.CommentsID = Integer.Parse(Request.QueryString("CommentsID"))
            _objComment.Status = _util.ConvertIntToBool(Integer.Parse(Request.QueryString("StatusID")))
            _objComment.UpdateStatus()
            'objComment.ArticleId = Integer.Parse(Request.QueryString("ArticlesId"))
            Response.Redirect("/mt-inside/PageComment.aspx", False)
        Else
            grvComments.DataSource = _objComment.GetAllPageComment()
            grvComments.DataBind()
        End If
    End Sub

    Private Sub btnApply_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnApply.Click
        For Each strID As String In Regex.Split(Request.Form("cboCheckLink"), ",")
            If Request.Form("selectedAction1") = "3" Or Request.Form("selectedAction0") = "3" Then
                _objComment.CommentsID = Integer.Parse(strID)
                _objComment.GetByID()
                _objComment.Status = False
                _objComment.UpdateStatus()
                _objComment.WriteAricleComment(1, _objComment.ArticleId.ToString)
            ElseIf Request.Form("selectedAction1") = "1" Or Request.Form("selectedAction0") = "1" Then
                _objComment.CommentsID = Integer.Parse(strID)
                _objComment.GetByID()
                _objComment.Status = True
                _objComment.UpdateStatus()
                _objComment.WriteAricleComment(1, _objComment.ArticleId.ToString)
            ElseIf Request.Form("selectedAction1") = "2" Or Request.Form("selectedAction0") = "2" Then
                _objComment.CommentsID = Integer.Parse(strID)
                _objComment.GetByID()
                _objComment.Delete()
                _objComment.WriteAricleComment(1, _objComment.ArticleId.ToString)
            End If
        Next
        Response.Redirect("/mt-inside/PageComment.aspx", False)
    End Sub

    Private Sub btnApply2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnApply2.Click
        btnApply_Click(sender, e)
    End Sub

    Public Function WriteAction(ByVal status As String, ByVal CommentsID As String, ByVal ArticlesId As String) As String
        Dim strOut As New StringBuilder()
        If status = "True" Then
            strOut.Append("[<a href=""PageComment.aspx?action=UpdateStatus&StatusID=0&ArticlesId=").Append(ArticlesId).Append("&CommentsID=").Append(CommentsID).Append(""" title=""Approve"">Approve</a> | ")
        Else
            strOut.Append("[<a href=""PageComment.aspx?action=UpdateStatus&StatusID=1&ArticlesId=").Append(ArticlesId).Append("&CommentsID=").Append(CommentsID).Append(""" title=""Unapprove"">Unapprove</a> | ")
        End If
        strOut.Append("<a onclick=""javascript:return confirm('Are you sure ?');"" style=""color:#FF0000;"" href=""PageComment.aspx?action=Delete&ArticlesId=").Append(ArticlesId).Append("&CommentsID=").Append(CommentsID).Append(""" title=""Delete"">Delete</a> | ")
        strOut.Append("<a href=""CommentUpdate.aspx?action=Edit&ArticlesId=").Append(ArticlesId).Append("&CommentsID=").Append(CommentsID).Append(""" title=""Edit"">Edit</a> | ")
        strOut.Append("<a href=""CommentUpdate.aspx?action=Reply&ArticlesId=").Append(ArticlesId).Append("&CommentsID=").Append(CommentsID).Append(""" title=""Reply"">Reply</a> | ")
        strOut.Append("<a onclick=""javascript:return confirm('Are you sure ?');"" style=""color:#FF5100;"" href=""CommentUpdate.aspx?action=Spam&CommentsID=").Append(CommentsID).Append(""" title=""Spam"">Spam</a>]")
        Return strOut.ToString()
    End Function

    Private Sub grvComments_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles grvComments.PageIndexChanging
        grvComments.PageIndex = e.NewPageIndex
        BindGrid()
    End Sub

End Class